﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace CivitaiAPI
{
    public class HTML2Text
    {
        public static string Html2Text(string html)
        {
            var xmldoc = Html2XmlDocument(html);
            return xmldoc.InnerText;
            //var xdoc = ToXDocument(xmldoc);
            //using (StringWriter sr = new StringWriter())
            //{
            //    Html2Text(xdoc, sr);
            //    return sr.ToString();
            //} 
        }

        public static void Html2Text(XDocument source, TextWriter output)
        {

            Transformer.Transform(source.CreateReader(), null, output);
        }

        public static XslCompiledTransform _transformer;
        public static XslCompiledTransform Transformer
        {
            get
            {
                if (_transformer == null)
                {
                    _transformer = new XslCompiledTransform();
                    var xsl = XDocument.Parse(@"<?xml version='1.0'?><xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" exclude-result-prefixes=""xsl""><xsl:output method=""html"" indent=""yes"" version=""4.0"" omit-xml-declaration=""yes"" encoding=""UTF-8"" /><xsl:template match=""/""><xsl:value-of select=""."" /></xsl:template></xsl:stylesheet>");
                    _transformer.Load(xsl.CreateNavigator());
                }
                return _transformer;
            }
        }


        private static XmlDocument Html2XmlDocument(string html)
        {
            XmlDocument doc = null;
            using (TextReader textReader = new StringReader(html))
            {
                Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader()
                {
                    DocType = "HTML",
                    WhitespaceHandling = WhitespaceHandling.All,
                    CaseFolding = Sgml.CaseFolding.ToLower,
                    InputStream = textReader
                };
                doc = new XmlDocument()
                {
                    PreserveWhitespace = true,
                    XmlResolver = null
                };
                doc.Load(sgmlReader);
            }


            // create document

            return doc;
        }

        private static XmlDocument ToXmlDocument(XDocument xDocument)
        {
            var xmlDocument = new XmlDocument();
            using (var xmlReader = xDocument.CreateReader())
            {
                xmlDocument.Load(xmlReader);
            }
            return xmlDocument;
        }

        private static XDocument ToXDocument(XmlDocument xmlDocument)
        {
            using (var nodeReader = new XmlNodeReader(xmlDocument))
            {
                nodeReader.MoveToContent();
                return XDocument.Load(nodeReader);
            }
        }

        //static void Main(string[] args)
        //{
        //    var html = XDocument.Parse("<html><body><div>Hello world!</div></body></html>");
        //    var text = Html2Text(html);
        //    Console.WriteLine(text);
        //}
    }
}
